package com.zt.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.zt.anno.LogOperation;
import com.zt.excel.StudentListener;
import com.zt.pojo.*;
import com.zt.service.StudentService;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    // 分页查询
    @GetMapping
    public Result QueryPage(StudentsQuery studentsQuery){
        PageResult pageResult = studentService.QueryPage(studentsQuery);
        return Result.success(pageResult);
    }
    //删除学员
    @DeleteMapping("/{id}")
    @LogOperation
    public Result deleteById(@PathVariable Integer[] id){
        studentService.deleteById(id);
        return Result.success();
    }
    //增加学员
    @PostMapping
    @LogOperation
    public Result add(@RequestBody Student student){
        studentService.add(student);
        return Result.success();
    }
    //根据学员id查询
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Student student = studentService.findById(id);
        return Result.success(student);
    }
    //修改学员
    @PutMapping
    @LogOperation
    public Result update(@RequestBody Student student){
        studentService.update(student);
        return Result.success();
    }
    //违纪处理
    @PutMapping("/violation/{id}/{score}")
    @LogOperation
    public Result punish(@PathVariable Integer id,@PathVariable Integer score){
            studentService.updatescore(id,score);
        return Result.success();
    }

    //导入学生信息
    @PostMapping("/import/{clazzId}")
    @LogOperation
    public Result importStudent(MultipartFile file, @PathVariable Integer clazzId) throws IOException {
        EasyExcel.read(file.getInputStream(),
                        StudentModel.class,
                        new StudentListener(clazzId,studentService))
                .sheet().doRead();
        return Result.success();
    }

    /**
     * 导出学生信息
     */
    @GetMapping("/export")
    @LogOperation
    public void exportStudent(HttpServletResponse response, String name, Integer degree, Integer clazzId) throws IOException {
        List<StudentModel> studentModelList = studentService.list(name, degree, clazzId);
        //response.setHeader("Content-Disposition", "attachment;filename=student.xlsx");
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

        EasyExcel.write(response.getOutputStream(), StudentModel.class)
                .sheet("学员信息列表")
                .registerWriteHandler(new HorizontalCellStyleStrategy(null, writeCellStyle))
                .doWrite(studentModelList);
    }

}
